package com.xxx.shetuan3.controller;

import com.xxx.shetuan3.common.CommonResult;
import com.xxx.shetuan3.pojo.Organization;
import com.xxx.shetuan3.pojo.StudentAndOrganization;
import com.xxx.shetuan3.pojo.User;
import com.xxx.shetuan3.service.StudentAndOrganizationService;
import com.xxx.shetuan3.service.StudentService;
import com.xxx.shetuan3.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ApiOperation("查询个人信息")
    @GetMapping("/{studentId}")
    public CommonResult<User> findStudentById(@PathVariable Integer studentId){
        User user = studentService.findUserByUserId(studentId);
        return CommonResult.success(user);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/{studentId}")
    public CommonResult updateUser(@PathVariable("studentId") Integer studentId,User user){
        user.setUserId(studentId);
        studentService.updateStudent(user);
        return CommonResult.success();
    }

    @ApiOperation("报名社团")
    @PostMapping("/organization")
    public CommonResult joinOrganization(StudentAndOrganization studentAndOrganization){
        System.out.println(studentAndOrganization);
        studentAndOrganization.setState("审核中");
        studentService.joinOrganization(studentAndOrganization);

        return CommonResult.success("报名成功");
    }

    @ApiOperation("查询所有（未参加的）社团")
    @GetMapping("/organizations/unaccepted")
    public CommonResult<List<StudentAndOrganization>> queryAllUnacceptedOrganizations( @RequestParam(value = "studentId") Integer studentId,
                                                                                       @RequestParam(value = "start", defaultValue = "1") Integer start,
                                                                                       @RequestParam(value = "size", defaultValue = "10") Integer size){
        List<StudentAndOrganization> studentAndOrganizationList = studentService.queryAllUnacceptedOrganizations(studentId,start,size);
        return CommonResult.success(studentAndOrganizationList);
    }

    @ApiOperation("查询已参加的社团")
    @GetMapping("/organizations/joined")
    public CommonResult<List<Organization>> queryAllJoinedOrganization(     @RequestParam(value = "studentId") Integer studentId,
                                                                            @RequestParam(value = "start", defaultValue = "1") Integer start,
                                                                            @RequestParam(value = "size", defaultValue = "10") Integer size){
        List<Organization> organizationList = studentService.queryAllJoinedOrganizations(studentId, start, size);
        return CommonResult.success(organizationList);
    }

    @ApiOperation("查询所有社团")
    @GetMapping("/organizations/page")
    public CommonResult<List<Organization>> pageOrganizations( @RequestParam(value = "start", defaultValue = "1") Integer start,
                                                               @RequestParam(value = "size", defaultValue = "10") Integer size){
        List<Organization> organizationList = studentService.queryAllOrganizations(start,size);
        return CommonResult.success(organizationList);
    }

    @ApiOperation("退出社团")
    @DeleteMapping("/organziations")
    public CommonResult exitOrganization(Integer studentId, Integer organizationId){
        boolean res = studentService.exitOrganization(studentId,organizationId);
        return CommonResult.success();
    }

}